package com.wzy.authority.controller;

import com.github.pagehelper.Page;
import com.wzy.authority.base.dto.SysUserDto;
import com.wzy.authority.base.request.PageParameter;
import com.wzy.authority.base.result.Results;
import com.wzy.authority.domain.SysUser;
import com.wzy.authority.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @program: authorityManagement
 * @description: 用户控制器
 * @author: 1
 * @create: 2020-03-13 15:40
 **/
@Slf4j
@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private SysUserService sysUserServiceImpl;


    //必须拥有sys:user:query权限的角色才可以访问
    @ResponseBody
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:user:query')")
    public Results<Page<SysUser>> getUserList(PageParameter pageParameter, SysUserDto sysUserDto) {
        return sysUserServiceImpl.selectUserByPage(pageParameter, sysUserDto);
    }


    @GetMapping("/add")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public String getPageView(Model model) {
        model.addAttribute(new SysUser());
        return "user/user-add";
    }


    @GetMapping("/edit/{userId}")
    @PreAuthorize("hasAuthority('sys:user:edit')")
    public String getPageView(Model model,@PathVariable  Integer userId) {
        model.addAttribute("sysUser",sysUserServiceImpl.selectUserById(userId));
        return "user/user-edit";
    }

    @ResponseBody
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public Results<SysUser> saveUser(SysUserDto sysUserDto) {
        if (sysUserDto.getRoleId() != null) {
            //密码加密
            sysUserDto.setPassword(passwordEncoder.encode(sysUserDto.getPassword()));
            //添加用户
            return sysUserServiceImpl.saveUser(sysUserDto);
        }
        return Results.failure();
    }

    @ResponseBody
    @PostMapping("/edit")
    @PreAuthorize("hasAuthority('sys:user:edit')")
    public Results editUser(SysUserDto sysUserDto){
        if(sysUserDto.getRoleId() != null){
            return sysUserServiceImpl.updateSysUser(sysUserDto);
        }
        return Results.failure();
    }

    @ResponseBody
    @PostMapping("/delete/{userId}")
    @PreAuthorize("hasAuthority('sys:user:del')")
    public Results deleteUser(@PathVariable  Integer userId){
        if(userId != null){
            return sysUserServiceImpl.deleteUser(userId);
        }

        return Results.failure();
    }

    String pattern = "yyyy-MM-dd";
    @InitBinder
    public void initBinder(WebDataBinder webDataBinder, WebRequest webRequest){
        webDataBinder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat(pattern),true));
    }

}
